import cv2
import numpy as np
import random

def show(image,name="image"):
    print("show %s start"%name)

    h = image.shape[0]
    w = image.shape[1]

    # cv2.namedWindow('window', cv2.WINDOW_AUTOSIZE)
    cv2.imshow("%s,h=%d,w=%d"%(name,h,w), image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    print("show %s end"%name)

def cut_image(cv_image, cut_h, cut_w):
    # 目标尺寸，160*220
    h, w, c = cv_image.shape

    h_resize = cut_h
    w_resize = int(h_resize * (w / h))

    if w_resize < cut_w:
        # 如果按照高的标准缩小后，宽度小于目标宽度。则宽度置为目标宽度，高度相应比例缩小
        w_resize = cut_w
        h_resize = int(w_resize * (h / w))

    cv_image = cv2.resize(cv_image, (w_resize, h_resize), interpolation=cv2.INTER_NEAREST)

    y0 = 0
    y1 = cut_h

    x0 = int((w_resize - cut_w) / 2)
    x1 = x0 + cut_w

    cv_image = cv_image[y0:y1, x0:x1]  # 裁剪坐标为[y0:y1, x0:x1]
    # cv2.imwrite(filename, cv_image)
    return cv_image

def mergeImage_with_seamless():
    """
    使用seamlessClone实现图像无缝融合效果
    :return:
    """

    # Read images
    src = cv2.imread("data/airplane.jpg")
    dst = cv2.imread("data/sky.jpg")

    # Create a rough mask around the airplane.

    src_mask = np.zeros(src.shape, src.dtype)

    poly = np.array([[4, 80], [30, 54], [151, 63], [254, 37], [298, 90], [272, 134], [43, 122]], np.int32)

    cv2.fillPoly(src_mask, [poly], (255, 255, 255))

    center = (500, 300)

    output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)

    show(output,"seamless")

def mergeImage_with_add():



    target = cv2.imread('data/logo.png')
    bg = cv2.imread('data/sky.jpg')


    h, w, c = target.shape
    roi = bg[0:h, 0:w]

    # Now create a mask of logo and create its inverse mask also
    targetGray = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
    show(targetGray,"targetGray")

    _, mask = cv2.threshold(targetGray, 200, 255, cv2.THRESH_BINARY)
    mask_inv = cv2.bitwise_not(mask)
    show(mask,"mask")
    show(mask_inv,"mask_inv")

    bg_roi = cv2.bitwise_and(roi, roi,mask=mask)
    target_roi = cv2.bitwise_and(target, target,mask=mask_inv)

    show(bg_roi,"bg_roi")
    show(target_roi,"target_roi")

    # Put logo in ROI and modify the main image
    dst = cv2.add(bg_roi, target_roi)
    show(dst, "dst")

    # bg[0:h, 0:w] = dst
    bg[0:h, 0:w] = target

    show(bg,"bg")


def create_image():
    image = np.zeros((480,720,3),dtype=np.uint8)

    image[:,:,0] = 255
    image[:,:,1] = 255
    image[:,:,2] = 255

    h,w,c = image.shape
    # show(image,"create")

    target = cv2.imread("data/airplane.jpg")
    target = cv2.resize(target,(0,0),fx=0.4,fy=0.4)

    target_h,target_w,target_c = target.shape

    # show(plane,"plane")

    change_h = h - target_h
    change_w = w - target_w

    y0 = random.randint(0,change_h)
    x0 = random.randint(0,change_w)


    image[y0:y0+target_h,x0:x0+target_w] = target

    show(image, "create")




if __name__ == '__main__':
    # mergeImage_with_add()
    mergeImage_with_seamless()
    # create_image()
